//
// Created by Semir on 2020/6/7.
//

#include <iostream>
#include <string>
#include <stack>

using namespace std;

void Reverse(string& str, int begin, int end) {
    if (begin >= end) {
        return;
    }
    char temp;
    while (begin < end) {
        temp = str[begin];
        str[begin] = str[end];
        str[end] = temp;
        begin++;
        end--;
    }
}

string ReverseSentence(string str) {
    if (str.empty() || str == " ") {
        return str;
    }
    string res = str;
    Reverse(res, 0, res.length() - 1);
    int i = 0;
    while (res[i] == ' ') {
        i++;
    }
    int begin = i;
    while (i < res.length()) {
        if (res[i] == ' ' || res[i] == '\t') {
            Reverse(res, begin, i - 1);
            begin = i + 1;
        }
        i++;
    }
    Reverse(res, begin, i - 1);
    return res;
}

int main() {
    string str = ReverseSentence("   java  ");
    cout << str << endl;
    return 0;
}